package com.umer.pinban.user;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.utils.AntiShake;
import com.common.utils.Constants;
import com.common.utils.GlideUtils;
import com.umer.pinban.AppHolder;
import com.umer.pinban.HomeActivity;
import com.umer.pinban.R;
import com.umer.pinban.bean.user.SelectBuyMember;
import com.umer.pinban.bean.user.SubmitBuyMemberBean;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 扫码支付
 */
public class SweepCodePayActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    private String paymentNo = "";
    private String qrPath = "";
    private String remark= "";
    private String price = "";
    private String content = "";
    private UserInfo userInfo;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        //handleMessage为处理消息的方法
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            if (what == 100) {
                selectBuyMember();
            }

        }
    };
    private Timer timer = new Timer();
    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            Message message = new Message();
            message.what=100;
            handler.sendMessage(message);
        }
    };

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_sweep_code_pay;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        paymentNo = getIntent().getStringExtra("paymentNo");
        qrPath = getIntent().getStringExtra("qrPath");
        remark = getIntent().getStringExtra("remark");
        price = getIntent().getStringExtra("price");
        content = getIntent().getStringExtra("content");
        tv1.setText("交易单号:" + paymentNo);
        tv2.setText(content + "," + remark + "." +"(" + "¥" + price +")");
        GlideUtils.getInstance().loadImageView(this, Constants.IP_PORT_DEFAULT_PICTURE +qrPath,ivCode);
        timer.scheduleAtFixedRate(timerTask,0, 2000);
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.selectBuyMember:
                SelectBuyMember selectBuyMember = (SelectBuyMember) t.getT();
                if(selectBuyMember!=null){
                    int approveStatus = selectBuyMember.getApproveStatus();
                    if(approveStatus == 1){
                        switchTo(SweepCodePayActivity.this,PaySuccessActivity.class,true);
                    }
                }
                break;
            default:break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void selectBuyMember(){
        OkEntityRequest<SelectBuyMember> request = new OkEntityRequest<>(R.id.selectBuyMember,
                Constants.selectBuyMember, SelectBuyMember.class);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("paymentNo", paymentNo);
        request.setHeader(true);
        requestOkhttpEntity(request);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLoopViewPager();
        handler.removeCallbacksAndMessages(null);
    }

    private void stopLoopViewPager() {
        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }
}
